package cn.mumzone.pos.presenter;

import java.util.HashMap;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.basic.util.log.Elog;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.event.InOrOutStockEvent;
import cn.mumzone.pos.event.MoreViewChangeEvent;
import cn.mumzone.pos.model.RepoListGoodsEntity;
import cn.mumzone.pos.model.RepositoryEntity;
import cn.mumzone.pos.model.source.GoodsRepository;
import cn.mumzone.pos.model.source.StockRepository;
import cn.mumzone.pos.presenter.contract.StockOutContract;
import cn.mumzone.pos.presenter.contract.StockOutSearchContract;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by qiuzhenhuan on 2019/7/31.
 */

public class StockOutPresenter extends AbsPresenter<StockOutContract.View, StockRepository> implements StockOutContract.Presenter {

    private int mPageNum = 1;

    GoodsRepository goodsRepository;

    @Override
    protected void initOtherRepository() {
        super.initOtherRepository();
        goodsRepository = new GoodsRepository();
    }


    public StockOutPresenter() {
        Subscription subscribe = RxBus.getDefault().toObservable(InOrOutStockEvent.class)
                .subscribe(new Action1<InOrOutStockEvent>() {
                    @Override
                    public void call(InOrOutStockEvent inOrOutStockEvent) {
                        mView.onGoodsAdded(inOrOutStockEvent.getEntity());
                    }

                });

        addSubscrebe(subscribe);

        Subscription moreViewChangeListener = RxBus.getDefault()
                .toObservable(MoreViewChangeEvent.class)
                .subscribe(new Action1<MoreViewChangeEvent>() {
                    @Override
                    public void call(MoreViewChangeEvent moreViewChangeEvent) {
                        mView.onBackPressed(moreViewChangeEvent.getType());
                    }
                });
        addSubscrebe(moreViewChangeListener);
    }

    @Override
    public void loadRepositoryList() {

        ApiCallBack<PageListResponse<RepositoryEntity>> callBack = new ApiCallBack<PageListResponse<RepositoryEntity>>() {
            @Override
            public void onSuccess(PageListResponse<RepositoryEntity> response) {
                mView.showRepositoryList(response.getList());
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };

        mRepo.getRepositoryListPage(callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void loadRepoGoodsById(String repositoryId, String keyword) {
        ApiCallBack<PageListResponse<RepoListGoodsEntity>> callBack = new ApiCallBack<PageListResponse<RepoListGoodsEntity>>() {
            @Override
            public void onSuccess(PageListResponse<RepoListGoodsEntity> response) {
                if (!ListUtil.isListEmpty(response.getList())) {
                    mView.showScanGoodsDialog(response.getList());
                } else {
                    mView.showError("仓库内不存在该商品");
                }
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };

        mRepo.findRepoGoodsByRepoIdFromScan(repositoryId, keyword, "3", callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void generateOutBill(String warehouseId, String outType,
                                String outNum, String outAmount, String remark, String goodsJson) {
        ApiCallBack<Object> callBack = new ApiCallBack<Object>() {
            @Override
            public void onSuccess(Object response) {
                mView.onBillGenerated();
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("creater", AppConstants.TEMP_SELLER_ID);
        hashMap.put("shopName", AppConstants.TEMP_SHOP_NAME);
        hashMap.put("outStatus", "1");

        hashMap.put("warehouseId", warehouseId);
        hashMap.put("outType", outType);
        hashMap.put("remark", remark);
        hashMap.put("goodsList", goodsJson);

        mRepo.generateOutStockBill(callBack, hashMap);
        addSubscrebe(callBack);
    }

}
